﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using V5_BLL;
using V5_Common;
using V5_Model;

namespace V5_EShop._Class.Data {
    public class LoopHelper {
        public static LoopHelper Instance { get; set; } = new LoopHelper();
        //列表
        public dynamic List(string type, params string[] pp) {
            var rows = default(dynamic);
            switch (type) {
                case "announce-list": {
                        var row = int.Parse(pp[0]);
                        rows = new BLLAnnounce().GetModelList(row, "", "id desc");
                    }
                    break;

                #region 文章
                case "article-list": {
                        var row = int.Parse(pp[0]);
                        var strWhere = pp[1];

                        rows = new BLLArticle().GetModelList(row, strWhere, "id desc");
                    }
                    break;
                case "article-class": {
                        rows = new BLLArticleClass().GetDropDownList("", 0);
                    }
                    break;
                #endregion

                #region 产品
                case "product-list": {
                        pp.CheckPP(2);
                        var row = int.Parse(pp[0]);
                        var strWhere = pp[1];

                        rows = new BLLProduct().GetModelList(row, strWhere, "id desc");
                    }
                    break;
                case "product-hot": {
                        pp.CheckPP(1);
                        var row = int.Parse(pp[0]);
                        rows = new BLLProduct().GetModelList(row, "IsOnHot=1", "id desc");
                    }
                    break;
                case "product-class": {
                        DataTable dt = new BLLProductClass().GetDropDownList("", 0);
                        rows = dt.Rows;
                    }
                    break;
                case "product-cookies": {
                        var cUserBrowseProductList = "cUserBrowseProductList";
                        if (HttpContext.Current.Request.Cookies[cUserBrowseProductList] != null) {

                            string browseStr = InputHelper.SetString(HttpContext.Current.Request.Cookies[cUserBrowseProductList].Value);
                            string[] strArray = browseStr.Split(new char[] { ',' });

                            int row = int.Parse(pp[0]);

                            DataTable dt = new BLLProduct().GetList(row, " ID in(" + browseStr + ") ", " ID Desc").Tables[0];
                            rows = dt.Rows;
                        }
                    }
                    break;
                #endregion

                case "friend-list": {
                        var row = int.Parse(pp[0]);
                        rows = new BLLFriend().GetModelList(row, "", "id desc");
                    }
                    break;

                case "diy-list": {
                        pp.CheckPP(2);
                        var row = int.Parse(pp[0]);
                        var strWhere = pp[1];
                        DataTable dt = new BLLHelp().GetList(row, strWhere, "id desc").Tables[0];
                        dt.Columns.Add("Url");
                        foreach (DataRow dr in dt.Rows) {
                            dr["Url"] = "/diy/view/" + dr["id"].ToString();
                        }
                    }
                    break;
                case "hotkey": {
                        var row = int.Parse(pp[0]);
                        rows = new BLLHotKeyWord().GetList(row, "", " id asc ").Tables[0];
                    }
                    break;
                case "user-list": {
                        var row = int.Parse(pp[0]);
                        rows = new BLLUser().GetModelList(row, "", "id desc");
                    }
                    break;
            }
            return rows;
        }
    }

    public static class aa {
        public static void CheckPP(this string[] pp, int count) {
            if (pp.Count() != count) {
                throw new Exception($"参数不完整!需要参数个数为:{count}个");
            }
        }
    }
}